// packageProductInfo/page/payInfo/payInfo.js
import user from '../../../utils/user'
import {
    getAccountExpireNotify,
    getAccountProductInfo,
    getQueryRechargeRecordApi,
    getadphone,
    queryRechargeAndSync
} from '../../../api/app'
import dayjs from 'dayjs'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        phonelist: [],
        isShowTips: false,
        companyName: '',
        auditStatus: false,
        isOpenPay: false,
        isOpenAlipayAccount: false,
        expirationTime: {}, // 到期时间
        accountProductInfo: {}, // 卡包信息
        currentTab: 1,
        list: [],
        hasNext: true,
        pageNum: 1
    },
    scrollEnd() {
        if (!this.data.hasNext) {
            return
        }
        this.setData({
            page: this.data.pageNum += 1
        })
        if (this.data.currentTab === 2) {
            this.getListData()
        }
    },
    getListData() {
        getQueryRechargeRecordApi({
            pageNum: this.data.pageNum,
            pageSize: 10
        }).then((res) => {
            console.log(res)
            if (!res.hasNextPage) {
                this.setData({
                    hasNext: false
                })
            }
            const arr = []
            arr.push(...this.data.list)
            arr.push(...res.list.map((item) => {
                item.payMoney = item.rechargeAmount
                return item
            }))
            this.setData({
                list: arr
            })
        })
    },
    onAlipayRecharge() {
        wx.navigateTo({
            url: '/packageProductInfo/page/alipayRecharge/alipayRecharge',
        })
    },
    onMyRecharge(e) {
        console.log(e, user.insureCompanyType)
        if (e.currentTarget.dataset.type === 'app') {
            wx.navigateToMiniProgram({
                appId: e.currentTarget.dataset.path,
            })
        } else {
            if (user.insureCompanyType === 'insure001_wesure') {
                wx.showToast({
                    title: '功能维护中',
                    icon: 'none'
                })
                return
            } else {
                let str = e.currentTarget.dataset.type ? '?type=' + e.currentTarget.dataset.type : ''
                wx.navigateTo({
                    url: e.currentTarget.dataset.path + str
                })
            }

        }
    },
    // 提示信息
    onTips() {
        console.log('点击啦')
        this.setData({
            isShowTips: !this.data.isShowTips
        })
    },
    onChangeTab(e) {
        this.setData({
            pageNum: 1,
            list: [],
            hasNext: true,
            currentTab: e.currentTarget.dataset.i
        })
        if (e.currentTarget.dataset.i === 2) {
            this.getListData()
            wx.setNavigationBarColor({
                frontColor: '#000000',
                backgroundColor: '#eff7ff',
            })
        }
        if (e.currentTarget.dataset.i === 1) {
            wx.setNavigationBarColor({
                frontColor: '#000000',
                backgroundColor: '#f3fdfa',
            })
        }
    },
    callPhone1(phoneNumber) {
        wx.makePhoneCall({
            phoneNumber: phoneNumber,
            success: (res) => {
                console.log(111, res)
            },
            fail: function (res) {
                console.log(222, res)
            }
        })
    },
    onCallPhone() {
        let arr = []
        this.data.phonelist.forEach((ele, index) => {
            arr.push(ele.phone + '-' + ele.name)
        })
        wx.showActionSheet({
            itemList: arr,
            success: (res) => {
                this.callPhone1(this.data.phonelist[res.tapIndex].phone + '')
            },
            fail(res) {
                console.log(res.errMsg)
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options)
        if (Number(options.i) === 1) {
            wx.setNavigationBarColor({
                frontColor: '#000000',
                backgroundColor: '#f3fdfa',
            })
        }
        if (Number(options.i) === 2) {
            queryRechargeAndSync()
            wx.setNavigationBarColor({
                frontColor: '#000000',
                backgroundColor: '#eff7ff',
            })
        }
        this.setData({
            currentTab: Number(options.i)
        })
        getadphone().then((res) => {
            console.log(res, 'yfsudahiygiuyh')
            if (res.length === 0) {
                this.setData({
                    isnull: true,
                    phonelist: [{
                        name: '刘峰',
                        phone: '17177788882'
                    }]
                })

            } else {
                this.data.phonelist = res.map((ele, index) => {
                    return {
                        name: ele.salesName,
                        phone: ele.phone
                    }
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        user.waitLogin().then(async () => {
            this.setData({
                pageNum: 1,
                list: [],
                hasNext: true,
                companyName: user.companyName,
                auditStatus: user.auditStatus,
                isOpenPay: !!user.isOpenPaySalary,
                isOpenAlipayAccount: !!user.isOpenAlipayAccount,
            })
            await queryRechargeAndSync()
            await this.getAccountExpireNotify()
            await this.getAccountProductInfo()
            await this.getListData()
        })
    },
    async getAccountProductInfo() {
        const result = await getAccountProductInfo()
        this.setData({
            accountProductInfo: result
        })
    },
    // 账户到期提示接口
    async getAccountExpireNotify() {
        const result = await getAccountExpireNotify()
        result.accountExpireTime = result.accountExpireTime ? dayjs(result.accountExpireTime).format('YYYY/MM/DD').replace(/\//g, '-') : null
        this.setData({
            expirationTime: result
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})